import AjaxUtil from './ajaxUtil.js';


function getCity() {
	let ajax=new AjaxUtil();
	let url="user.servlet?param=city&code="+document.getElementByid("province").value;
	ajax.getJson(url,(xhr)=>{
		let json=xhr.response;
		let node=document.getElementById("city");
		node.innerHTML="";
		if(json.msg!="no data"){
			let defaultOption=document.createElement("option");
			defaultOption.value="0000";
			defaultOption.innerHTML="请选择城市";
			defaultOption.selected=true;
			node.appendChild(defaultOption);
			json.result.forEach((item)=>{
				let option=document.createElement("option");
				option.value=item.code;
				option.innerHTML=item.name;
				node.appendChild(option);
			});
			
		}
	});
}

function init(){
        var shu=document.querySelectorAll("input");
       [...shu].forEach((item)=>{
                item.addEventListener("click",(e)=>{
                        // console.log(e.target.id);
                         var id=e.target.id;
                         var regExp;
                         var msg;
                         var img;
                         var error_value;
                         var ok_value;
                         var errImg;
                         var okImg;
                        if(id=="userName"){
                            regExp=/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
                            msg=".user_error_msg";
                            img=".user_error_img";
                            error_value="用户名不符合规则!";
                            ok_value="用户名可以使用";
                            errImg="errImg";
                            okImg="okImg";
                        }else if(id=="Name"){
                            regExp=/^.{1,8}$/;
                            msg=".name_error_msg";
                            img=".name_error_img";
                            error_value="昵称不符合规则!";
                            ok_value="昵称可以使用";
                            errImg="errImg";
                            okImg="okImg";
                            
                        }else if(id=="password"){
                            regExp=/^[a-zA-Z0-9_.-]{4,12}$/;
                            msg=".password_error_msg";
                            img=".password_error_img";
                            error_value="密码不符合规则!";
                            ok_value="密码可以使用";
                            errImg="errImg";
                            okImg="okImg";
                        }else if(id=="email"){
                            regExp=/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
                            msg=".email_error_msg";
                            img=".email_error_img";
                            error_value="邮箱输入格式有误!";
                            ok_value="邮箱格式符合要求";
                            errImg="errImg";
                            okImg="okImg";
                        }else if(id=="phone"){
                            regExp=/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
                            msg=".phone_error_msg";
                            img=".phone_error_img";
                            error_value="手机号输入有误!";
                            ok_value="号码格式正确";
                            errImg="errImg";
                            okImg="okImg";
                        }

                        /**------------------------------------------------------------------------- */
                        if(id!="password01"){
                        document.getElementById(id).addEventListener("focus",()=>{
                            document.querySelector(msg).innerHTML="";
                            document.querySelector(msg).classList.remove("errorMesssage");
                            document.querySelector(msg).classList.remove("okMessage");
                            document.querySelector(img).classList.remove(errImg);
                            document.querySelector(img).classList.remove(okImg);
                            
                        },false);
                        document.getElementById(id).addEventListener("blur",()=>{
                            if(!regExp.test(document.getElementById(id).value)){
                                
                                document.querySelector(msg).innerHTML=error_value;
                                document.querySelector(msg).classList.add("errorMesssage");
                                document.querySelector(img).classList.add("acc_img");
                                document.querySelector(img).classList.add(errImg);
                            }else{
                               
                                document.querySelector(msg).innerHTML=ok_value;
                                document.querySelector(msg).classList.add("okMessage");
                                document.querySelector(img).classList.add("acc_img");
                                document.querySelector(img).classList.add(okImg);
                            };
                            
                        },false);
                    }
                        /**---------------------------------------------------------------------- */
                    
                        document.getElementById("password01").addEventListener("focus",()=>{
                            document.querySelector(".password01_error_msg").innerHTML="";
                            document.querySelector(".password01_error_msg").classList.remove("errorMesssage");
                            document.querySelector(".password01_error_msg").classList.remove("okMessage");
                            document.querySelector(".password01_error_img").classList.remove("errImg");
                            document.querySelector(".password01_error_img").classList.remove("okImg");
                            document.querySelector(".password01_error_img").classList.remove("acc_img");
                        },false);
                        document.getElementById("password01").addEventListener("blur",()=>{
                            // console.log(document.getElementById("password").value);
                            // console.log(document.getElementById("password01").value);
                            console.log((document.getElementById("password").value)==(document.getElementById("password01").value));
                            if(!((document.getElementById("password").value)==(document.getElementById("password01").value))){
                                
                                document.querySelector(".password01_error_msg").innerHTML="密码输入不同!";
                                document.querySelector(".password01_error_msg").classList.add("errorMesssage");
                                document.querySelector(".password01_error_img").classList.add("acc_img");
                                document.querySelector(".password01_error_img").classList.add("errImg");
                            }else{
                               
                                document.querySelector(".password01_error_msg").innerHTML="密码输入相同";
                                document.querySelector(".password01_error_msg").classList.add("okMessage");
                                document.querySelector(".password01_error_img").classList.add("acc_img");
                                document.querySelector(".password01_error_img").classList.add("okImg");
                            };
                            
                        },false);
                    });
        });
       document.getElementById("province").addEventListener("change",getCity,false);
}

window.addEventListener("load",init,false);